package com.glorystone.ins.task;

import com.glorystone.ins.domain.Advance;
import com.glorystone.ins.domain.BusinessLog;
import com.glorystone.ins.enums.FlowBusinessTypeEnum;
import com.glorystone.ins.enums.FlowOperateTypeEnum;
import com.glorystone.ins.enums.WorkFlowEnum;
import com.glorystone.ins.service.AdvanceService;
import com.glorystone.ins.service.AuditTaskService;
import com.glorystone.ins.service.BusinessLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * @author:haoguodong
 * @Date:Created in 10:59 2018/3/21
 * @since: JDK 1.7
 * @projectName:IntelliJ IDEA
 * @Description:类功能描述
 */
@Service
@Slf4j
@Lazy(false)
public class AdvanceTask {

    @Autowired
    private AdvanceService advanceService;

    @Autowired
    private AuditTaskService auditTaskService;

    @Autowired
    private BusinessLogService businessLogService;

    @Scheduled(cron = "0 0 2 * * ?")
    public void refreshData(){
        log.info("------失效的数据-----refreshData-------begin");
        advanceService.updateStatus();
        log.info("------失效的数据------refreshData-------end");


        log.info("------今日垫资限额------insertData-------begin");
        Advance advance=new Advance();
        advance.setAuditStatus(Advance.AuditStatusEnum.CHECK_SUCCESS.getValue());
        advance.setStatus(1);
        advance.setCreateAt(LocalDateTime.now());
        advance.setTodatAmount(new BigDecimal(50000));
        advance.setActualAmount(new BigDecimal(0));
        advance.setDelFlg(Advance.DeleteFlagEnum.NORMAL.getValue());
        advanceService.saveAmount(advance);
        log.info("------今日垫资限额------insertData-------end");
    }

    @Scheduled(cron = "0 0 1 * * ?")
    public void clearData(){
        log.info("------清除失效的数据-----clearData-------begin");
        Advance advance=advanceService.findClearData();
        if(advance.getTid()!=null){
            advanceService.clearData();
            businessLogService.save(BusinessLog.BusinessTypeEnum.ADVANCE_LIMIT, advance.getTid(), BusinessLog.ContentEnum.ADVANCE_CHECK_REJECT, "失效数据");
            auditTaskService.submit(WorkFlowEnum.ADVANCE_LIMIT, FlowBusinessTypeEnum.ADVANCE_LIMIT, advance.getTid(), FlowOperateTypeEnum.REJECT);
        }
        log.info("------清除失效的数据------clearData-------end");

    }

}
